const supportService = require('../service/support.service');

class SupportController {

  // 点赞
  async support(ctx, next) {
    // 1.获取参数
    const [resourceKey] = Object.keys(ctx.params); // 操作的字段  momentId / commentId
    const tableName = resourceKey.replace("Id", ""); // 操作的表名  moment / comment
    const resourceId = ctx.params[resourceKey]; // 操作字段的值  1 / 1
    const { id } = ctx.user;

    // 2.查询点赞表
    const isSupport = await supportService.isSupport(tableName, resourceId, id);
    if (isSupport) {
      // 已点赞, 需取消点赞
      // 1.更新 点赞表 / 动态表
      const result = await supportService.unSupport(tableName, resourceId, id);
      ctx.body = result;
    } else {
      // 未点赞, 需点赞
      // 1.更新 点赞表 / 动态表
      const result = await supportService.toSupport(tableName, resourceId, id);
      ctx.body = result;
    }
  }

}

module.exports = new SupportController();
